package api

import (
	"errors"
	"time"
)

type ProviderType string

const (
	Wechat   ProviderType = "wechat"
	Douyin   ProviderType = "douyin"
	Kuaishou ProviderType = "kuaishou"
	QQ       ProviderType = "qq"
)

var (
	ErrUnsupportedApi = errors.New("unsupported api")
)

type MpConfig struct {
	Provider  ProviderType `json:"provider" yaml:"provider"`
	AppId     string       `json:"app_id" yaml:"app_id"`
	AppSecret string       `json:"app_secret" yaml:"app_secret"`
	Sandbox   bool         `json:"sandbox,omitempty" yaml:"sandbox"` // TODO：目前仅支持抖音开放平台
	Spec      string       `json:"spec" yaml:"spec"`
}

type AccessToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"` //seconds
	ExpireAt    int64  `json:"expires_at"` //milliseconds
	TokenType   string `json:"token_type" yaml:"token_type"`
}

type Quota struct {
	DailyLimit int `json:"daily_limit"`
	Used       int `json:"used"`
	Remain     int `json:"remain"`
}

type Code2Session struct {
	SessionKey      string `json:"session_key"`
	Unionid         string `json:"unionid"`
	Openid          string `json:"openid"`
	AnonymousOpenid string `json:"anonymous_openid,omitempty"`
}

type PhoneNumber struct {
	PhoneNumber     string    `json:"phoneNumber"`     //用户绑定的手机号（国外手机号会有区号）
	PurePhoneNumber string    `json:"purePhoneNumber"` //没有区号的手机号
	CountryCode     int       `json:"countryCode"`     //区号
	Watermark       Watermark `json:"watermark"`       //数据水印
}

type Watermark struct {
	Timestamp int64  `json:"timestamp"` //用户获取手机号操作的时间戳
	Appid     string `json:"appid"`     //小程序appid
}

type WxTextCheckResp struct {
	Result  WxTextCheckResult   `json:"result"`
	Detail  []WxTextCheckDetail `json:"detail"`
	TraceID string              `json:"trace_id"`
}

type WxTextCheckResult struct {
	Suggest string `json:"suggest"`
	Label   int    `json:"label"`
}

type WxTextCheckDetail struct {
	Strategy string `json:"strategy"`
	Errcode  int    `json:"errcode"`
	Suggest  string `json:"suggest"`
	Label    int    `json:"label"`
	Prob     int    `json:"prob,omitempty"`
	Level    int    `json:"level,omitempty"`
	Keyword  string `json:"keyword,omitempty"`
}

type DyTextCheckResp struct {
	LogID string            `json:"log_id"`
	Data  []DyTextCheckData `json:"data"`
}
type DyTextCheckPredict struct {
	Prob      int    `json:"prob"`
	Hit       bool   `json:"hit"`
	Target    string `json:"target"`
	ModelName string `json:"model_name"`
}
type DyTextCheckData struct {
	Msg      string               `json:"msg"`
	Code     int                  `json:"code"`
	TaskID   string               `json:"task_id"`
	Predicts []DyTextCheckPredict `json:"predicts"`
	DataID   string               `json:"data_id"`
}

func (at *AccessToken) Calc() {
	at.ExpireAt = time.Now().Add(time.Second * time.Duration(at.ExpiresIn)).UnixMilli()
}

func (at *AccessToken) Expired() bool {
	return at.ExpireAt < time.Now().UnixMilli()
}
